{
 "cells": [
  {
   "cell_type": "raw",
   "id": "0a35e0d7-9257-479c-8d72-728fc2940961",
   "metadata": {},
   "source": [
    "激活函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e1069abb-1242-4da9-9d7e-94a876a5c987",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "207f83e3-ee09-4b0e-9081-195fd37b3eaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "input = torch.randn(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6f224911-0c31-4196-8403-21501623ff2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.6317, -0.9155,  0.6361, -0.5252, -0.1275])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0161e025-33af-43a5-99db-0918876400b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.6529, 0.2859, 0.6539, 0.3716, 0.4682])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sigmoid 输出0-1之间的数\n",
    "torch.sigmoid(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b21fde4-48b7-4606-92b7-104af58bc848",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.6317, 0.0000, 0.6361, 0.0000, 0.0000])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# relu \n",
    "torch.relu(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b92e4534-df02-4381-afb1-78105a636621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.5592, -0.7237,  0.5622, -0.4817, -0.1268])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tanh 输出 -1 1之间\n",
    "torch.tanh(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2066e180-c630-41cf-86b2-a91b59b5e14a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4e4b5b54-97fe-48b3-a1c2-812acaa6c4a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LeakyReLU(negative_slope=tensor([ 0.6317, -0.9155,  0.6361, -0.5252, -0.1275]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# leak relu 待泄露的\n",
    "nn.LeakyReLU(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ca0eae4-22a2-4b21-84b8-be64a363593a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
